package com.mlh.exception;

import org.springframework.web.bind.annotation.ControllerAdvice;
import org.springframework.web.bind.annotation.ExceptionHandler;

import javax.servlet.http.HttpServletRequest;

@ControllerAdvice
public class MyGlobalException {

    // 1.全局异常就不管是哪个Handler抛出的异常，都可以捕获，@ExceptionHandler({异常类型})
    // 2.这里处理的全局异常是NumberFormatException.class , ClassCastException.class
    @ExceptionHandler({NumberFormatException.class, ClassCastException.class})
    public String globalExcption(Exception exception, HttpServletRequest request){
        System.out.println("全局异常处理"+exception.getMessage());
        request.setAttribute("reason",exception.getMessage());
        return "exception_mes";
    }

    @ExceptionHandler(value = AgeException.class)
    public String globalExcption2(Exception exception, HttpServletRequest request){
        System.out.println("全局异常处理"+exception.getMessage());
        request.setAttribute("reason",exception.getMessage());
        return "exception_mes";
    }
}
